package com.lingCheng.avg.service.impl;

import com.lingCheng.avg.common.SheBeiData;
import com.lingCheng.avg.entity.WcsTrk;
import com.lingCheng.avg.mapper.WcsTrkMapper;
import com.lingCheng.avg.service.WcsTrkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class WcsTrkServiceImpl implements WcsTrkService {
    @Autowired
    private WcsTrkMapper wcsTrkMapper;

    @Override
    public List<SheBeiData> findDeviceError() {
        WcsTrk cvinError = wcsTrkMapper.findCVINError();
        WcsTrk srmError = wcsTrkMapper.findSRMError();
        WcsTrk agvError = wcsTrkMapper.findAGVError();

        SheBeiData cvinErrorData = new SheBeiData();
        cvinErrorData.setName("输送线");
        if (cvinError == null) {
            cvinErrorData.setValue("0");
        } else {
            cvinErrorData.setValue(cvinError.getValue());
        }

        SheBeiData srmErrorData = new SheBeiData();
        srmErrorData.setName("堆垛机");
        if (srmError == null) {
            srmErrorData.setValue("0");
        } else {
            srmErrorData.setValue(srmError.getValue());
        }

        SheBeiData agvErrorData = new SheBeiData();
        agvErrorData.setName("AGV");
        if (agvError == null) {
            agvErrorData.setValue("0");
        } else {
            agvErrorData.setValue(agvError.getValue());
        }

        List<SheBeiData> dataList = new ArrayList<>();
        dataList.add(cvinErrorData);
        dataList.add(srmErrorData);
        dataList.add(agvErrorData);
        // 按故障率从大到小排序
        dataList.sort(Comparator.comparing(SheBeiData::getValue).reversed());
        return dataList;
    }
}
